home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / tplib21.zip / INSTALL.EXE / HELPCONV.PAS < prev    next >
Pascal/Delphi Source File  |  1993-06-24  |  5KB  |  170 lines

  1. (*
  2.     TURBO PASCAL LIBRARY 2.1
  3.     HELPCONV: File conversion utility for help system.
  4. *)
  5.  
  6. PROGRAM HELPCONV(InFile,OutFile,Output);
  7.  
  8. {$V-}
  9.  
  10. USES
  11.     DOS,STDERR,CRTCLERR,STRINGS;
  12.  
  13. TYPE
  14.     HelpIndexString = STRING[30];               { String to hold help index }
  15.  
  16.     HelpRec     =   RECORD                      { Record format in help file }
  17.                         SecNum: BYTE;
  18.                         CASE INTEGER OF
  19.                             0:      (SecOfs:    LONGINT;
  20.                                      SecLength: WORD;
  21.                                      SecName:   HelpIndexString);
  22.                             1:      (HelpText:  STRING[80]);
  23.                     END;
  24.  
  25. CONST
  26.     SectionDelim = '#';
  27.     NumberDelim  = ':';
  28.  
  29.  
  30. VAR
  31.     InFile:         TEXT;                               { Input text file }
  32.     OutFile:        FILE OF HelpRec;                    { Output file }
  33.     HelpVar:        HelpRec;
  34.     IndexSize:      BYTE;                               { Number of sections }
  35.     HelpIndexStr:   ARRAY[1..255] OF HelpIndexString;   { Index phrases }
  36.     HelpSecOfs:     ARRAY[1..255] OF LONGINT;           { Offset to sections }
  37.     HelpSecLength:  ARRAY[1..255] OF WORD;              { Length sections }
  38.     s:              STRING;
  39.     d:              DirStr;
  40.     e:              ExtStr;
  41.     i,j:            BYTE;
  42.     LineCount:      LONGINT;
  43.     x:              INTEGER;
  44.  
  45.  
  46. PROCEDURE FormatError;
  47.  
  48.     VAR
  49.         s:  STRING[20];
  50.  
  51.     BEGIN
  52.         STR(LineCount,s);
  53.         WriteStdErr(CONCAT('ERROR: Input-file format error at line ',s));
  54.         CLOSE(InFile);
  55.         CLOSE(OutFile);
  56.         HALT(1);
  57.     END;
  58.  
  59.  
  60. PROCEDURE ReadNextLine;
  61.  
  62.     BEGIN
  63.         IF EOF(InFile) THEN
  64.             BEGIN
  65.                 WriteStdErr('ERROR:  Unexpected end of input file');
  66.                 CLOSE(InFile);
  67.                 CLOSE(OutFile);
  68.                 HALT(1);
  69.             END;
  70.         READLN(InFile,s);
  71.     END;
  72.  
  73.  
  74. BEGIN
  75.     WRITELN('HELPCONV version 2.1 - File conversion utility for help system.');
  76.     WRITELN('Copyright (C) 1991, 1993  Paul Coxwell.  ',
  77.                     'All rights reserved.', #10);
  78.     IF ParamCount=0 THEN
  79.         BEGIN
  80.             WRITELN('Command syntax:',#10);
  81.             WRITELN('  HELPCONV input-file [output-file]',#10);
  82.             WRITELN('Default output-file is same path and name as input,',
  83.                      ' but with "HLP" extension.',#10);
  84.             EXIT;
  85.         END;
  86.     ASSIGN(InFile,ParamStr(1));
  87.     {$I-}
  88.     RESET(InFile);                      { Open input file }
  89.     {$I+}
  90.     IF IOResult<>0 THEN
  91.         BEGIN
  92.             WriteStdErr('ERROR: Cannot open input file');
  93.             HALT(1);
  94.         END;
  95.     FSplit(ParamStr(1),d,s,e);
  96.     s:=FileSpecDefault(ParamStr(2),d,s,'.HLP');
  97.     ASSIGN(OutFile,s);
  98.     {$I-}
  99.     REWRITE(OutFile);                   { Open output file }
  100.     {$I+}
  101.     IF IOResult<>0 THEN
  102.         BEGIN
  103.             WriteStdErr(CONCAT('ERROR: Cannot open output file ',s));
  104.             CLOSE(InFile);
  105.             HALT(1);
  106.         END;
  107.     WriteLn('Creating help file ',s);
  108.     WriteLn;
  109.     ReadNextLine;                       { Get number of sections }
  110.     LineCount:=1;
  111.     VAL(s,IndexSize,x);
  112.     IF (x<>0) OR (IndexSize<1) THEN
  113.         FormatError;
  114.     WITH HelpVar DO
  115.         BEGIN
  116.             SecNum:=0;
  117.             SecOfs:=-1;
  118.             SecLength:=0;
  119.             SecName:='';
  120.         END;
  121.     FOR i:=1 TO IndexSize DO            { Clear array variables, write empty }
  122.         WRITE(OutFile,HelpVar);           { index records to file }
  123.     REPEAT
  124.         ReadNextLine;
  125.         INC(LineCount);
  126.     UNTIL (LENGTH(s)>0) AND (s[1]=SectionDelim);
  127.     FOR i:=1 TO IndexSize DO            { Read & convert each section's text }
  128.         BEGIN
  129.             VAL(Precede(Follow(s,SectionDelim),NumberDelim),j,x);
  130.             IF (x<>0) OR (j<>i) THEN
  131.                 FormatError;
  132.             HelpSecOfs[i]:=FilePos(OutFile);
  133.             HelpIndexStr[i]:=Follow(s,NumberDelim);
  134.             HelpSecLength[i]:=0;
  135.             REPEAT
  136.                 ReadNextLine;
  137.                 INC(LineCount);
  138.                 IF (LENGTH(s)=0) OR (s[1]<>SectionDelim) THEN
  139.                     BEGIN
  140.                         WITH HelpVar DO
  141.                             BEGIN
  142.                                 SecNum:=i;
  143.                                 HelpText:=s;
  144.                             END;
  145.                         WRITE(OutFile,HelpVar);
  146.                         INC(HelpSecLength[i]);
  147.                         IF HelpSecLength[i]>1000 THEN
  148.                             FormatError;
  149.                     END;
  150.             UNTIL (LENGTH(s)>0) AND (s[1]=SectionDelim);
  151.         END;
  152.     SEEK(OutFile,0);
  153.     FOR i:=1 TO IndexSize DO            { Build index in file }
  154.         BEGIN
  155.             WITH HelpVar DO
  156.                 BEGIN
  157.                     SecNum:=0;
  158.                     SecOfs:=HelpSecOfs[i];
  159.                     SecLength:=HelpSecLength[i];
  160.                     SecName:=HelpIndexStr[i];
  161.                 END;
  162.             WRITE(OutFile,HelpVar);
  163.         END;
  164.     CLOSE(InFile);
  165.     CLOSE(OutFile);
  166.     WRITELN('Conversion successful');
  167.     WRITELN(LineCount,' lines processed, ',IndexSize,' sections.');
  168. END.
  169.  
  170.